package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.entity.CheckItemException;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/5-22:10
 * @Version: 1.0.0
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
//        分页查询 , 传入当前页码, 一页多少条
        PageHelper.startPage(currentPage, pageSize);
//        调用服务查询总条数 , 用page接受
        Page<CheckItem> page= checkItemMapper.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
//        查询关联表记录条数,如果存在引用无法删除
        Integer count = checkItemMapper.findCountById(id);
        if(count>0){
            throw new CheckItemException("该项存在引用,无法删除");
        }else{
            checkItemMapper.deleteById(id);
        }
    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem= checkItemMapper.findById(id);
        return checkItem;
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {

        List<CheckItem> checkItems = checkItemMapper.findAll();
        return checkItems;
    }
}
